home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2001 Spring / Oh!X 2001 Spring Special CD-ROM (Japan).7z / Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin / JAVA / jshooting3 / jshooting.java < prev    next >
Text File  |  2000-08-18  |  9KB  |  338 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.lang.Math;
  5. import java.util.Random;
  6.  
  7. public class jshooting extends Applet implements KeyListener, ActionListener, Runnable {
  8.     static final int WIDTH = 240, HEIGHT = 320;    // 背景サイズ
  9.     static final int SHOTNUM = 5;    // 弾の数
  10.     static final int ENEMYNUM = 10;    // 敵の数
  11.     static final int BULLETNUM = 20;    // 敵弾の数
  12.     static final int SHOT = 1;    // 弾のプレーン番号
  13.     static final int ENEMY = SHOT+SHOTNUM;    // 敵のプレーン番号
  14.     static final int BULLET = ENEMY+ENEMYNUM;    // 敵弾のプレーン番号
  15.     SpriteCanvas sc;    // スプライトキャンバス
  16.     MediaTracker mt;
  17.     boolean up=false, down=false, left=false, right=false, zkey=false;
  18.     int mx, my;
  19.     int zkeyblank;
  20.     Thread thread=null;
  21.     Label Score;
  22.     Button StartBtn;
  23.     int score=0;
  24.     int bgwidth[] = { WIDTH };
  25.     int bgheight[] = { HEIGHT };
  26.     int bgnum[] = { 1 };
  27.     int scroll = 0;
  28.     boolean shot[] = new boolean[SHOTNUM];
  29.     int shot_x[] = new int[SHOTNUM], shot_y[] = new int[SHOTNUM];
  30.     boolean enemy[] = new boolean[ENEMYNUM];
  31.     int enemy_x[] = new int[ENEMYNUM], enemy_y[] = new int[ENEMYNUM];
  32.     boolean bullet[] = new boolean[BULLETNUM];
  33.     int bullet_x[] = new int[BULLETNUM], bullet_y[] = new int[BULLETNUM];
  34.     int bullet_dx[] = new int[BULLETNUM], bullet_dy[] = new int[BULLETNUM];
  35.     boolean play = false;
  36.     Random aRandom = new Random();
  37.     Math aMath;
  38.     public void init(){
  39.         // パターン 背景1、自機1、弾1、敵1、敵弾1、爆発1、合計6
  40.         // プレーン 自機1、弾5、敵10、敵弾20、合計36
  41.         sc = new SpriteCanvas( 6, 36, WIDTH, HEIGHT, this );
  42.         setLayout( new BorderLayout() );
  43.         Score = new Label( "SCORE "+score, Label.CENTER );
  44.         StartBtn = new Button( "Start" );
  45.         add( "Center", sc );
  46.         add( "North", Score );
  47.         add( "South", StartBtn );
  48.         mt = new MediaTracker( this );
  49.         Image image = getImage( getDocumentBase(), "img/back.gif" );    // 背景
  50.         mt.addImage( image, 0 );
  51.         sc.Define( 0, image );
  52.         // バックグラウンド1プレーン
  53.         sc.CreateBackground( 1, bgwidth, bgheight, bgnum );
  54.         sc.SetBGPattern( 0, 0, 0, 0, 0 );
  55.         sc.BGShow();
  56.         image = getImage( getDocumentBase(), "img/myship.gif" );    // 自機
  57.         mt.addImage( image, 0 );
  58.         sc.Define( 1, image );
  59.         image = getImage( getDocumentBase(), "img/shot.gif" );    // 弾
  60.         mt.addImage( image, 0 );
  61.         sc.Define( 2, image );
  62.         image = getImage( getDocumentBase(), "img/enemy.gif" );    // 敵
  63.         mt.addImage( image, 0 );
  64.         sc.Define( 3, image );
  65.         image = getImage( getDocumentBase(), "img/bullet.gif" );    // 敵弾
  66.         mt.addImage( image, 0 );
  67.         sc.Define( 4, image );
  68.         image = getImage( getDocumentBase(), "img/bomb.gif" );    // 爆発
  69.         mt.addImage( image, 0 );
  70.         sc.Define( 5, image );
  71.         sc.WaitLoadImage( mt, 0 );
  72.         StartBtn.addKeyListener( this );
  73.         StartBtn.addActionListener( this );
  74.     }
  75.     public void GameStart(){
  76.         int i;
  77.         // 自機のセット
  78.         sc.Set( 0, 1 );
  79.         mx = (WIDTH-32)/2;
  80.         my = HEIGHT-32*2;
  81.         sc.Move( 0, mx, my );
  82.         play = true;
  83.         score = 0;
  84.         sc.Hide();
  85.         for( i=0; i<5; i++ ) shot[i] = false;
  86.         for( i=0; i<10; i++ ) enemy[i] = false;
  87.         for( i=0; i<20; i++ ) bullet[i] = false;
  88.         Score.setText( "SCORE "+score );
  89.         sc.Show( 0, 0 );
  90.     }
  91.     public void keyPressed( KeyEvent e ){
  92.         switch( e.getKeyCode() ){
  93.         case KeyEvent.VK_UP:
  94.             up = true;
  95.             break;
  96.         case KeyEvent.VK_DOWN:
  97.             down = true;
  98.             break;
  99.         case KeyEvent.VK_LEFT:
  100.             left = true;
  101.             break;
  102.         case KeyEvent.VK_RIGHT:
  103.             right = true;
  104.             break;
  105.         case KeyEvent.VK_Z:
  106.             if( zkey==false ){
  107.                 zkey = true;
  108.                 zkeyblank = 0;
  109.             }
  110.             break;
  111.         }
  112.     }
  113.     public void keyReleased( KeyEvent e ){
  114.         switch( e.getKeyCode() ){
  115.         case KeyEvent.VK_UP:
  116.             up = false;
  117.             break;
  118.         case KeyEvent.VK_DOWN:
  119.             down = false;
  120.             break;
  121.         case KeyEvent.VK_LEFT:
  122.             left = false;
  123.             break;
  124.         case KeyEvent.VK_RIGHT:
  125.             right = false;
  126.             break;
  127.         case KeyEvent.VK_Z:
  128.             zkey = false;
  129.             break;
  130.         }
  131.     }
  132.     public void keyTyped( KeyEvent e ){
  133.     }
  134.     public void actionPerformed( ActionEvent e ){
  135.         if( e.getActionCommand().equalsIgnoreCase( "Start" ) ){
  136.             if( play==false ){    // ゲームスタート
  137.                 GameStart();
  138.             }
  139.         }
  140.     }
  141.     public void start(){
  142.         if( thread==null ){
  143.             thread = new Thread( this );
  144.             thread.start();
  145.         }
  146.     }
  147.     public void stop(){
  148.         if( thread!=null ){
  149.             thread = null;
  150.         }
  151.     }
  152.     public void run(){
  153.         int i, j;
  154.         try {
  155.             mt.waitForID( 0 );
  156.         } catch( InterruptedException e ){
  157.             return;
  158.         }
  159.         while( thread!=null ){
  160.             try {
  161.                 Thread.sleep( 50 );
  162.             } catch( InterruptedException e ){
  163.                 break;
  164.             }
  165.             if( play ){
  166.                 if( up ){
  167.                     my -= 4;
  168.                     if( my<0 ) my = 0;
  169.                 }
  170.                 if( down ){
  171.                     my += 4;
  172.                     if( my>HEIGHT-32 ) my = HEIGHT-32;
  173.                 }
  174.                 if( left ){
  175.                     mx -= 4;
  176.                     if( mx<0 ) mx = 0;
  177.                 }
  178.                 if( right ){
  179.                     mx += 4;
  180.                     if( mx>WIDTH-32 ) mx = WIDTH-32;
  181.                 }
  182.                 sc.Move( 0, mx, my );
  183.                 // 弾の発射
  184.                 if( zkey ){
  185.                     // 押しっぱなしのブランク期間
  186.                     if( zkeyblank==0 ){
  187.                         // 空きを検索
  188.                         for( i=0; i<SHOTNUM; i++ ){
  189.                             if( shot[i]==false ) break;
  190.                         }
  191.                         // 弾の設定
  192.                         if( i<SHOTNUM ){
  193.                             shot_x[i] = mx;
  194.                             shot_y[i] = my;
  195.                             sc.Set( i+SHOT, 2 );
  196.                             sc.Show( i+SHOT, i+SHOT );
  197.                             shot[i] = true;
  198.                         }
  199.                     }
  200.                     zkeyblank = (zkeyblank+1)%4;
  201.                 }
  202.                 // 敵の出現
  203.                 if( (aRandom.nextInt()&0xf)==0 ){
  204.                     // 空きを検索
  205.                     for( i=0; i<ENEMYNUM; i++ ){
  206.                         if( enemy[i]==false ) break;
  207.                     }
  208.                     // 敵の設定
  209.                     if( i<ENEMYNUM ){
  210.                         enemy_x[i] = Math.abs(aRandom.nextInt())%(WIDTH-32);
  211.                         enemy_y[i] = -32;
  212.                         sc.Set( i+ENEMY, 3 );
  213.                         sc.Show( i+ENEMY, i+ENEMY );
  214.                         enemy[i] = true;
  215.                     }
  216.                 }
  217.                 // 敵の移動
  218.                 for( i=0; i<ENEMYNUM; i++ ){
  219.                     // 表示されている敵の検索
  220.                     if( enemy[i] ){
  221.                         // 移動
  222.                         enemy_y[i] += 4;
  223.                         // 画面外判定
  224.                         if( enemy_y[i]>=HEIGHT ){
  225.                             sc.Hide( i+ENEMY, i+ENEMY );
  226.                             enemy[i] = false;
  227.                         } else {
  228.                             sc.Move( i+ENEMY, enemy_x[i], enemy_y[i] );
  229.                             // 敵弾発射
  230.                             if( (aRandom.nextInt()&0xf)==0 ){
  231.                                 // 空きを検索
  232.                                 for( j=0; j<BULLETNUM; j++ ){
  233.                                     if( bullet[j]==false ) break;
  234.                                 }
  235.                                 // 敵弾の設定
  236.                                 if( j<BULLETNUM ){
  237.                                     // 背後からの攻撃は禁止
  238.                                     if( my>enemy_y[i] ){
  239.                                         double l = aMath.sqrt((mx-enemy_x[i])*(mx-enemy_x[i])+(my-enemy_y[i])*(my-enemy_y[i]));
  240.                                         // 至近距離からの発射は禁止
  241.                                         if( l>100.0 ){
  242.                                             bullet_x[j] = enemy_x[i];
  243.                                             bullet_y[j] = enemy_y[i];
  244.                                             bullet_dx[j] = (int)aMath.round( 6*(mx-enemy_x[i])/l );
  245.                                             bullet_dy[j] = (int)aMath.round( 6*(my-enemy_y[i])/l );
  246.                                             sc.Set( j+BULLET, 4 );
  247.                                             sc.Show( j+BULLET, j+BULLET );
  248.                                             bullet[j] = true;
  249.                                         }
  250.                                     }
  251.                                 }
  252.                             }
  253.                             // 自機との当り判定
  254.                             if( enemy_x[i]>mx-18 && enemy_x[i]<mx+18 ){
  255.                                 if( enemy_y[i]>my-18 && enemy_y[i]<my+18 ){
  256.                                     // 敵爆発
  257.                                     sc.Set( i+ENEMY, 5 );
  258.                                     // 自機爆発
  259.                                     sc.Set( 0, 5 );
  260.                                     enemy[i] = false;
  261.                                     score += 10;
  262.                                     Score.setText( "SCORE "+score );
  263.                                     play = false;
  264.                                 }
  265.                             }
  266.                         }
  267.                     } else {
  268.                         sc.Hide( i+ENEMY, i+ENEMY );
  269.                     }
  270.                 }
  271.                 // 弾の移動
  272.                 for( i=0; i<SHOTNUM; i++ ){
  273.                     // 表示されている弾の検索
  274.                     if( shot[i] ){
  275.                         // 移動
  276.                         shot_y[i] -= 16;
  277.                         // 画面外判定
  278.                         if( shot_y[i]<=-32 ){
  279.                             sc.Hide( i+SHOT, i+SHOT );
  280.                             shot[i] = false;
  281.                         } else {
  282.                             sc.Move( i+SHOT, shot_x[i], shot_y[i] );
  283.                             // 敵との当り判定
  284.                             for( j=0; j<ENEMYNUM; j++ ){
  285.                                 if( enemy[j] ){
  286.                                     if( shot_x[i]>enemy_x[j]-12 && shot_x[i]<enemy_x[j]+12 ){
  287.                                         if( shot_y[i]>enemy_y[j]-12 && shot_y[i]<enemy_y[j]+12 ){
  288.                                             // 弾消去
  289.                                             sc.Hide( i+SHOT, i+SHOT );
  290.                                             shot[i] = false;
  291.                                             // 敵爆発
  292.                                             sc.Set( j+ENEMY, 5 );
  293.                                             enemy[j] = false;
  294.                                             score += 10;
  295.                                             Score.setText( "SCORE "+score );
  296.                                         }
  297.                                     }
  298.                                 }
  299.                             }
  300.                         }
  301.                     }
  302.                 }
  303.                 // 敵弾の移動
  304.                 for( i=0; i<BULLETNUM; i++ ){
  305.                     // 表示されている敵弾の検索
  306.                     if( bullet[i] ){
  307.                         // 移動
  308.                         bullet_x[i] += bullet_dx[i];
  309.                         bullet_y[i] += bullet_dy[i];
  310.                         // 画面外判定
  311.                         if( bullet_x[i]<=-32 || bullet_x[i]>=WIDTH ||
  312.                             bullet_y[i]<=-32 || bullet_y[i]>=HEIGHT ){
  313.                             sc.Hide( i+BULLET, i+BULLET );
  314.                             bullet[i] = false;
  315.                         } else {
  316.                             sc.Move( i+BULLET, bullet_x[i], bullet_y[i] );
  317.                             if( bullet_x[i]>mx-12 && bullet_x[i]<mx+12 ){
  318.                                 if( bullet_y[i]>my-12 && bullet_y[i]<my+12 ){
  319.                                     // 敵弾消去
  320.                                     sc.Hide( i+BULLET, i+BULLET );
  321.                                     // 自機爆発
  322.                                     sc.Set( 0, 5 );
  323.                                     play = false;
  324.                                 }
  325.                             }
  326.                         }
  327.                     }
  328.                 }
  329.                 // 背景スクロール
  330.                 scroll += 2;
  331.                 if( scroll>=HEIGHT ) scroll = 0;
  332.                 sc.BGScroll( 0, 0, scroll );
  333.             }
  334.             sc.Draw();    // スプライト描画
  335.         }
  336.     }
  337. }
  338.